package com.heliang.dataaccess.dao.impl;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import com.heliang.dataaccess.bean.InfoBean;
import com.heliang.dataaccess.dao.BaseDao;
import com.heliang.dataaccess.dao.InfoDao;
import com.hl.component.exception.DaoException;
import com.hl.component.exception.ErrorCode;
import com.hl.component.exception.handle.ExceptionHandle;

public class InfoDaoImpl extends BaseDao implements InfoDao {

    public InfoDaoImpl(Context context) {
        super(context);
    }

    public boolean deleteInfoByIds(Integer[] idArrays) throws DaoException {
        // TODO Auto-generated method stub
        return false;
    }

    public boolean saveInfos(List<InfoBean> infoBeans) throws DaoException {
        try {
            for (int i = 0; i < infoBeans.size(); i++) {
                InfoBean infoBean = infoBeans.get(i);
                infoBean.setRead(false);
                finalDb.save(infoBean);
            }
            return true;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_SAVE_ERROR_001, "InfoDaoImpl.saveInfos()"
                + "批量保存资讯信息失败！");
            return false;

        }
    }

    public boolean delete(int type, int catId, Integer[] idArray) throws DaoException {
        System.out.println("delete");
        boolean result = false;
        String sqlWhere = "";
        try {
            if (idArray == null || idArray.length == 0) {
                return result;
            }
            if (type == 1) {
                sqlWhere = "   TYPE = " + type + " AND INFOID IN(";
            } else {
                sqlWhere = "   TYPE = " + type + " AND CATID= " + catId + " AND INFOID IN(";
            }
            for (int i = 0; i < idArray.length; i++) {
                if (i != (idArray.length - 1)) {
                    sqlWhere += idArray[i] + ",";
                } else {
                    sqlWhere += idArray[i] + ")";
                }
            }

            finalDb.deleteByWhere(InfoBean.class, sqlWhere);
            return true;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_SAVE_ERROR_001, "InfoDaoImpl.delete()"
                + "根据资讯类别ID,位置(首页,非首页)删除资讯信息失败！");
            return false;
        }

    }

    /**
     * 获取记录总数
     * @param type 类型，1为首页，0为非首页
     * @param catId 分类ID
     * @return
     */
    public int queryInfoCount(int type, int catId) throws DaoException {
        List<InfoBean> infoBeans = new ArrayList<InfoBean>();
        try {
            String sqlStr = "";
            if (type == 1) {
                sqlStr = "   TYPE = 1 ";
            } else if (catId != -1) {
                sqlStr = "   TYPE=0 AND CATID= " + catId;
            }

            infoBeans = finalDb.findAllByWhere(InfoBean.class, sqlStr);
            return infoBeans.size();
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004,
                "InfoDaoImpl.queryInfoCount()" + "根据资讯类别ID,位置(首页,非首页)统计资讯信息条数失败！");
            return 0;
        }
    }

    /**
     * 删除最旧的n数据，包括图片附件
     * @param n
     * @return
     */
    public boolean deleteOldest(int type, int catId, int n) throws DaoException {
        System.out.println("deleteOldest");
        String ids = "";
        String strWhere = "";
        try {
            // 根据资讯类别ID,位置按时间先后顺序查询前n条数据
            List<InfoBean> infoBeans = finalDb.findAllByWhere(InfoBean.class, " TYPE = " + type
                + " AND CATID=" + catId + " ", " INFOID ASC ");

            /* 构建要删除资讯信息IDs */
            if (infoBeans != null && infoBeans.size() >= n) {
                int i = 0;
                for (InfoBean infoBean : infoBeans) {
                    if (i == infoBeans.size() - 1) {
                        ids += infoBean.getId() ;
                    } else {
                        ids += infoBean.getId() + ",";
                    }
                    i++;
                }
                strWhere = "  INFOID IN ( " + ids + ") ";
                
                /* 根据IDS删除资讯 */
                finalDb.deleteByWhere(InfoBean.class, strWhere);
            }
          
            return true;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_DELETE_ERROR_002,
                "InfoDaoImpl.deleteOldest()" + "删除最旧n条资讯信息条数失败！");
            return false;
        }
    }

    /**
     * 查询数据
     * @param type 用于区别首页与非首页资讯,1是首页，0是非首页
     * @param catId 分类ID
     * @return
     */
    public List<InfoBean> query(int type, int catId) throws DaoException {
        List<InfoBean> hotList = new ArrayList<InfoBean>();
        try {
            String strWhere = "";
            if (type == 1) {
                strWhere = "   TYPE=" + type + " ";
            } else if (catId != -1) {
                strWhere = "   TYPE=" + type + " AND CATID =" + catId + " ";
            }
            
            System.out.println(finalDb.findAll(InfoBean.class).size());
            hotList = finalDb.findAllByWhere(InfoBean.class, strWhere, "UPDATETIME");
            return hotList;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004, "InfoDaoImpl.query()"
                + "根据资讯类别ID,位置(首页,非首页)查询资讯信息条数失败！");
            return null;
        }
    }

    public boolean updateInfoBean(InfoBean infoBean) throws DaoException {
        try {
            // finalDb.update(InfoBean.class, "ID = "+ infoBean.getId()+" ");
            finalDb.update(infoBean);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004, "InfoDaoImpl.query()"
                + "更新资讯信息失败！");
            return false;
        }
    }

}
